/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.decode;

import me.panpf.sketch.util.SketchUtils;
import ohos.media.image.common.PixelFormat;

/**
 * {@link me.panpf.sketch.Sketch} 明确支持的图片格式
 */
public enum ImageType {
    JPEG("image/jpeg", PixelFormat.RGB_565, PixelFormat.RGB_565),
    PNG("image/png", PixelFormat.ARGB_8888, PixelFormat.ARGB_8888),
    WEBP("image/webp", PixelFormat.ARGB_8888, PixelFormat.ARGB_8888),
    GIF("image/gif", PixelFormat.ARGB_8888, PixelFormat.ARGB_8888),
    BMP("image/bmp", PixelFormat.RGB_565, PixelFormat.RGB_565),
    ;

    String mimeType;
    PixelFormat bestConfig;
    PixelFormat lowQualityConfig;

    ImageType(String mimeType, PixelFormat bestConfig, PixelFormat lowQualityConfig) {
        this.mimeType = mimeType;
        this.bestConfig = bestConfig;
        this.lowQualityConfig = lowQualityConfig;
    }

    public static ImageType valueOfMimeType(String mimeType) {
        if (ImageType.JPEG.mimeType.equalsIgnoreCase(mimeType)) {
            return ImageType.JPEG;
        } else if (ImageType.PNG.mimeType.equalsIgnoreCase(mimeType)) {
            return ImageType.PNG;
        } else if (ImageType.WEBP.mimeType.equalsIgnoreCase(mimeType)) {
            return ImageType.WEBP;
        } else if (ImageType.GIF.mimeType.equalsIgnoreCase(mimeType)) {
            return ImageType.GIF;
        } else if (ImageType.BMP.mimeType.equalsIgnoreCase(mimeType)) {
            return ImageType.BMP;
        } else {
            return null;
        }
    }

    public PixelFormat getConfig(boolean lowQualityImage) {
        return lowQualityImage ? lowQualityConfig : bestConfig;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setBestConfig(PixelFormat bestConfig) {
        //noinspection ConstantConditions
        if (bestConfig != null) {
            this.bestConfig = bestConfig;
        }
    }

    public void setLowQualityConfig(PixelFormat lowQualityConfig) {
        //noinspection ConstantConditions
        if (lowQualityConfig != null) {
//            if (lowQualityConfig == PixelFormat.ARGB_8888 && SketchUtils.isDisabledARGB4444()) {
//                lowQualityConfig = PixelFormat.ARGB_8888;
//            }
            this.lowQualityConfig = lowQualityConfig;
        }
    }

    public boolean equals(String mimeType) {
        return this.mimeType.equalsIgnoreCase(mimeType);
    }
}